(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.companyRating.createController', createController);
	createController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'companyRatingService', '$filter' ];
	function createController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, companyRatingService, $filter) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.companyRating = {};
		ctrl.companyRating.ratingtypes = ctrl.companyRating.ratingtypes || [];
		ctrl.showTabFlag = false;
		ctrl.showFlag = false;
		ctrl.init = function() {
			
			authService.create(params).then(function(response) {
				ctrl.companyRating = response.data || params;
				ctrl.companyRating.voRatingType = ctrl.companyRating.ratingTypes[0] || [];
				ctrl.ratingTypeOptions = ctrl.companyRating.ratingTypeOptions || [];
			});
		};
		ctrl.ratingTypeChanged = function() {
			companyRatingService.getLayoutForCompanyRating(ctrl.companyRating).then(function(response) {
				layoutConfig.formConfigs.ratingQaItems = {};
				layoutConfig.formConfigs.ratingQaAdjs = {};
				//console.log(angular.toJson(response.data,2));
				ctrl.layoutConfig = angular.merge(layoutConfig, response.data);
			}, angular.noop);
			var ratingType = ctrl.companyRating.ratingType;
			if(ratingType) {
				ctrl.showTabFlag = true;
				angular.forEach(ctrl.companyRating.ratingTypes, function(ratingTypeObj, index) {
					var _ratingType = $filter('encode')(ctrl.companyRating.ratingTypes[index].ratingType);
					if(ratingType == _ratingType) {
						ctrl.companyRating.voRatingType = ratingTypeObj;
						ctrl.qaItemMap = {};
						ctrl.qaItemMap = ctrl.companyRating.voRatingType.qaItemMap;
						//private Map<String, VoRatingQaItem> qaItemMap = new HashMap<String, VoRatingQaItem>();
						angular.forEach(ctrl.qaItemMap, function(ratingQaItemObj, index1) {
							var itemType = ratingQaItemObj.itemType;
							var isCategory = ratingQaItemObj.isCategory;
							if("Y"!=isCategory) {
								if("O" != itemType && "I" != itemType) {
									ctrl.qaItemMap[index1].checkboxChanged = function(){
										ctrl.qaItemMap[index1].usedItemType = 'O';
										if(ctrl.qaItemMap[index1].itemValues != null && ctrl.qaItemMap[index1].itemValues.length == 0 && ctrl.qaItemMap[index1].itemValue == ''){
											ctrl.qaItemMap[index1].readonlyFlag = false;
											ctrl.qaItemMap[index1]['readonlyFlag'+'_C'] = false;
										} else if(ctrl.qaItemMap[index1].itemValues != null && ctrl.qaItemMap[index1].itemValues.length > 0) {
											ctrl.qaItemMap[index1].readonlyFlag = true;
											ctrl.qaItemMap[index1]['readonlyFlag'+'_C']  = false;
										} else {
											ctrl.qaItemMap[index1].readonlyFlag = false;
											ctrl.qaItemMap[index1]['readonlyFlag'+'_C']  = true;
										}
									};
									ctrl.qaItemMap[index1].textChanged = function(){
										ctrl.qaItemMap[index1].usedItemType = 'I';
										if(ctrl.qaItemMap[index1].itemValues != null && ctrl.qaItemMap[index1].itemValues.length == 0 && ctrl.qaItemMap[index1].itemValue == ''){
											ctrl.qaItemMap[index1].readonlyFlag = false;
											ctrl.qaItemMap[index1]['readonlyFlag'+'_C']  = false;
										}else if(ctrl.qaItemMap[index1].itemValue != '') {
											ctrl.qaItemMap[index1].readonlyFlag = false;
											ctrl.qaItemMap[index1]['readonlyFlag'+'_C']  = true;
										} else {
											ctrl.qaItemMap[index1].readonlyFlag = true;
											ctrl.qaItemMap[index1]['readonlyFlag'+'_C']  = false;
										}
									};
								} else {
									ctrl.qaItemMap[index1].checkboxChanged = function(){
										ctrl.qaItemMap[index1].usedItemType = 'O';
									};
									ctrl.qaItemMap[index1].textChanged = function(){
										ctrl.qaItemMap[index1].usedItemType = 'I';
									};
								}
								ctrl.qaItemMap[index1].dataOptions = ratingQaItemObj.itemOptions;
							}
						});
						ctrl.qaAdjMap = {};
						ctrl.qaAdjMap = ctrl.companyRating.voRatingType.qaAdjMap;
						angular.forEach(ctrl.qaAdjMap, function(ratingQaAdjObj, index1) {
							ctrl.qaAdjMap[index1].dataOptions = ratingQaAdjObj.adjOptions;
						});
					}
				});
			}
		};
		ctrl.createSave = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.companyRating.taskId) {
				ctrl.showFlag = true;
				return authService.createContinueSave(ctrl.companyRating).then(saveSuccessful, saveFailed);
			} else {
				ctrl.showFlag = true;
				return authService.createSave(ctrl.companyRating).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createSubmit = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.companyRating.taskId) {
				return authService.createContinueSubmit(ctrl.companyRating).then(submitSuccessful, submitFailed);
			} else {
				return authService.createSubmit(ctrl.companyRating).then(submitSuccessful, submitFailed);
			}
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.companyRating.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.companyrating.list',
					'state' : 'cda.companyRating.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
